package com.digitalchina.lxnsy.activity.old;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessCaf;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.AgriSpecialInfoVO;
import com.digitalchina.lxnsy.entity.CafProblemVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.CafMainHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/10/27 13:58
 * 修改备注：
 * ================================================
 */
public class CafMainActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    @Bind(R.id.agricultural_experts_iv_information)
    TextView agriculturalExpertsIvInformation;

    private View headView;//头布局
    private SimpleDraweeView simpleDraweeView; // 顶部图片
    private TextView tvIntro;

    private RecyclerArrayAdapter adapter;
    private List<CafProblemVO> voList = new ArrayList<>();
    private AgriSpecialInfoVO agriSpecialInfo;


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_caf_main);
        setTitle("农高会专题");
    }

    @Override
    public void initMember() {

        agriculturalExpertsIvInformation.setVisibility(View.VISIBLE);

        headView = LayoutInflater.from(this).inflate(R.layout.view_caf_main, null);
        simpleDraweeView = (SimpleDraweeView) headView.findViewById(R.id.caf_main_sdv);
        tvIntro = (TextView) headView.findViewById(R.id.caf_main_tv_intro);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new CafMainHolder(parent);
            }
        });


        adapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {

                return headView;
            }

            @Override
            public void onBindView(View headerView) {


            }
        });

        recyclerView.setAdapter(adapter);

        onRefresh();
    }

    @Override
    public void addListener() {

        final String userId = "57ad8e58-4a8c-4ca1-9c65-1642e65ed91a";
        final String userName = "董智峰";
        final String userHead = URL.SERVER + "static/data-service/img/disease1.png";

        agriculturalExpertsIvInformation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (!Utils.isLogin(CafMainActivity.this)){
                    Utils.notLoginGoto(CafMainActivity.this);
                    return;
                }

            }
        });

        recyclerView.setRefreshListener(this);
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                List<CafProblemVO> list = adapter.getAllData();
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setSelect(i == position);
                }
                GotoUtil.gotoActivity(CafMainActivity.this, CafListActivity.class, "list", list);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);
                switch (msg.what) {

                    case MSG.MSG_AGRI_SPECIAL_INFO_SUCCESS:
                        callBack(msg.obj);
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "problemList", CafProblemVO.class);
        agriSpecialInfo = FastJsonUtil.getBean(object.toString(), "agriSpecialInfo", AgriSpecialInfoVO.class);

        if (voList != null && voList.size() > 0)
            adapter.clear();
        adapter.addAll(voList);

        if (agriSpecialInfo == null)
            return;

        if (!StringUtil.isStrEmpty(agriSpecialInfo.getSpecialImg()))
            FrescoUtil.showImageMid(agriSpecialInfo.getSpecialImg(), simpleDraweeView);

        tvIntro.setText(agriSpecialInfo.getSpecialProfile() + "");
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        BusinessCaf.agriSpecialInfo(this, mHandler);
    }
}
